package com.yiip.mr.custominput;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileSplit;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

/**
 * 自定义的recordreader类
 */
public class WholeRecordReader extends RecordReader<Text, BytesWritable> {

    private FileSplit fileSplit;
    private Configuration configuration;
    private Text text;
    private BytesWritable bytesWritable;
    private FSDataInputStream fis;
    boolean isProgress = true;

    /**
     * 初始化方法
     * @param split
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {
        this.fileSplit = (FileSplit) split;
        this.configuration = context.getConfiguration();
    }

    /**
     * 核心逻辑处理。
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean nextKeyValue() throws IOException, InterruptedException {
        //这里根据实际业务需求来即可。放到二进制流中。所以直接用io操作即可。
        if (isProgress) {
            FileSystem fs = null;
            //获取一个fs对象
            Path path = fileSplit.getPath();
            fs = path.getFileSystem(configuration);

            //拿到文件
            fis = fs.open(path);

            //io读取文件
            byte[] buf = new byte[(int) fileSplit.getLength()];
            IOUtils.readFully(fis,buf,0,buf.length);

            //设置value
            bytesWritable.set(buf,0,buf.length);

            //设置key
            text.set(path.toString());

            IOUtils.closeStream(fis);

            isProgress = false;
            return true;
        }

        return false;
    }

    @Override
    public Text getCurrentKey() throws IOException, InterruptedException {
        return text;
    }

    @Override
    public BytesWritable getCurrentValue() throws IOException, InterruptedException {
        return bytesWritable;
    }

    @Override
    public float getProgress() throws IOException, InterruptedException {
        return 0;
    }

    @Override
    public void close() throws IOException {

    }
}
